const app = getApp()
const {
  requestDataparams
} = require('../../../utils/request.js');
var applet_park_api_url_imgUrl = app.globalData.applet_park_api_url_imgUrl;
var util = require('../../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrl: applet_park_api_url_imgUrl,
    top: 20,
    height: 40,
    left: 0,
    showModal2: false,
    detail: {},
    date: '',
    timeStart: '09:00',
    timeEnd: '20:00',
    userInfo: {},
    addressData: [],
    currentForeignType: '',
    currentForeignId: '',
    currentForeignName: '',
    btn_name: '',
    userPhone: '',
    userId: '',
    param: true,
    now: util.formatTime(new Date()),
    avatarUrl: 'http://221.226.72.214:9527/touxang.png'
  },
  goBack() {
    wx.navigateBack({
      delta: 2
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (wx.getStorageSync('USER_INFO') != '') {
      this.setData({
        avatarUrl: wx.getStorageSync('USER_INFO').avatarUrl
      })
    }
    if (options.btn_name) {
      this.setData({
        btn_name: options.btn_name
      })
    }

    if (options.userPhone) {
      this.setData({
        userPhone: options.userPhone
      })
    }
    if (options.userId) {
      this.setData({
        userId: options.userId
      })
    }

    if (options.btn_name) { //跳转拜访函页面
      let startTime = options.startTime
      let endTime = options.endTime
      wx.showToast({
        title: '您已被邀请,请填写拜访函',
        icon: 'none',
        duration: 5000,
        success: function () {
          wx.navigateTo({
            url: '../../../packageA/pages/VIewerLetter/VIewerLetter?userPhone=' + options.userPhone + '&userId=' + options.userId + '&startTime=' + startTime + '&endTime=' + endTime,
          })
        }
      })

    }

    this.initPage()
    this.getAdderss()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  // 获取地址
  getAdderss() {
    // 
    requestDataparams('/api/gates/address', {}, 'GET').then(response => {
      if (response.data.code == 0) {
        this.setData({
          addressData: response.data.data
        })
      }
    })
  },
  // 地址selector切换
  addressChange(e) {
    // currentForeignType: '',
    // currentForeignId: '',
    // currentForeignName: ''
    this.setData({
      // currentForeignType: this.data.addressData[e.detail.value].foreignType,
      currentForeignId: this.data.addressData[e.detail.value].id,
      currentForeignName: this.data.addressData[e.detail.value].name,
    })
  },
  // 获取首页信息
  initPage() {
    // let wxOpenId= wx.getStorageSync('OPENID')
    let wxOpenId = wx.getStorageSync("OPENID")
    requestDataparams('/api/visitors', {
      wxOpenId
    }, 'GET').then(response => {
      // 
      if (response.data.data) {
        wx.setStorageSync("TOKEN", response.data.data.token);
        this.setData({
          userInfo: response.data.data
        })
      } else {
        // wx.navigateTo({
        //   url: '../VIewerLetter/VIewerLetter',
        // })
      }
    })
  },
  // 邀请函日期
  dateChange(e) {
    if (e.detail.value < this.data.now.substring(0, 10)) {
      wx.showToast({
        title: '当前日期不可用',
        icon: 'none'
      })
      return
    }
    this.setData({
      date: e.detail.value
    })
  },
  // 邀请开始时间
  timeStartChange(e) {
    this.setData({
      timeStart: e.detail.value
    })
  },
  // 邀请结束时间
  timeEndChange(e) {
    this.setData({
      timeEnd: e.detail.value
    })
  },
  // // 地址
  // addressChange(e){
  //   this.setData({
  //     region: e.detail.value
  //   })
  // },
  ViewerRecord() {
    wx.navigateTo({
      url: '../ViewerRecord/ViewerRecord',
    })
  },
  visitotmanger() {
    wx.navigateTo({
      url: '../Visitormanagemen/Visitormanagemen',
    })
  },
  letter() {
    wx.navigateTo({
      url: '../VIewerLetter/VIewerLetter',
    })
  },
  staffSetUp() {
    wx.navigateTo({
      url: '../staffList/staffList',
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getAdderss()
    let top = app.globalData.navTop
    let height = app.globalData.navHeight
    let left = app.globalData.arrowLeft
    this.setData({
      top,
      height,
      left
    })
  },
  // 控制弹出层
  meetingdetail(e) {
    if ( this.data.addressData == '无权限') {
      wx.showToast({
        title: '对不起！您暂无权限发送邀请函！',
        duration: 3000,
        icon: 'none'
      })
      return
    }
    if(this.data.addressData.length==0){
      wx.showToast({
        title: '对不起！您的邀请权限下暂无闸机，请联系相关管理员！',
        duration: 3000,
        icon: 'none'
      })
      return
    }
    // detail.meetingimg = e.currentTarget.dataset.meetingimg;
    // detail.meetingname = e.currentTarget.dataset.meetingname;
    // detail.price = e.currentTarget.dataset.price;
    // detail.buildingname = e.currentTarget.dataset.buildingname;
    // detail.devices = e.currentTarget.dataset.devices;
    this.setData({
      showModal2: true
    });

  },
  // 关闭弹出层
  closedetail() {
    this.setData({
      showModal2: false
    });
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 分享邀请函
   */
  onShareAppMessage: function (options) {
    var userPhone = this.data.userInfo.mobile;
    var userId = this.data.userInfo.id;
    let timeStart = this.data.date + ' ' + this.data.timeStart + ':00'
    let timeEnd = this.data.date + ' ' + this.data.timeEnd + ':00'
    let nickName = this.data.userInfo.nickName;
    console.log(nickName)
    //设置菜单中的转发按钮触发转发事件时的转发内容
    var shareObj = {
      title: "邀请函", // 默认是小程序的名称(可以写slogan等)
      imageUrl: '', //自定义图片路径，可以是本地文件路径、代码包文件路径或者网络图片路径，支持PNG及JPG，不传入 imageUrl 则使用默认截图。显示图片长宽比是 5:4
      success: function (res) {
        // 转发成功之后的回调
        if (res.errMsg == 'shareAppMessage:ok') {}
      },
      fail: function () {
        // 转发失败之后的回调
        if (res.errMsg == 'shareAppMessage:fail cancel') {
          // 用户取消转发
        } else if (res.errMsg == 'shareAppMessage:fail') {
          // 转发失败，其中 detail message 为详细失败信息
        }
      },
    }
    // 来自页面内的按钮的转发
    if (options.from == 'button') {
      var eData = options.target.dataset;
      // 此处可以修改 shareObj 中的内容
      shareObj.path = '/pages/index/index?btn_name=' + eData.name + '&userPhone=' + userPhone + '&userId=' + userId + '&startTime=' + timeStart + '&endTime=' + timeEnd + '&nickName=' + nickName + '&timeStamp=' + Date.parse(new Date())/1000;
    }
    console.log(shareObj)
    this.closedetail();
    return shareObj;
  }
})